package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.BussItem;
import com.haisen.dataobject.entity.CallOut;
import com.haisen.dataobject.mapper.base.CallOutMapper;
import com.haisen.dataobject.query.CallOutQuery;
import com.haisen.poi.basic_poi.BigExcelUtil;
import com.haisen.service.CallOutService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CallOutServiceImpl implements CallOutService {

      @Autowired
      private CallOutMapper callOutMapper;

      @Autowired
      private SqlSessionFactory sqlSessionFactory;

      private InsertBatch<CallOut> batchAdd = null;


    @Override
    public void create(CallOut callOut) {
        callOutMapper.insertSelective(callOut);
    }

    @Override
    public void update(CallOut callOut) {
       callOutMapper.updateByPrimaryKeySelective(callOut);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<CallOut> example = Weekend.of(CallOut.class);
        WeekendCriteria<CallOut,Object> criteria = example.weekendCriteria();
        criteria.andIn(CallOut::getId, Arrays.asList(ids));
        callOutMapper.deleteByExample(example);
    }

    @Override
    public void creatbatch(List<CallOut> callOutList) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        MyMapper<CallOut> myMapper = (MyMapper<CallOut>) sqlSession.getMapper(CallOutMapper.class);
        batchAdd = new InsertBatch<>();
       // log.info("开始batchadd,数量={}", JSON.toJSONString(callOutList));
        batchAdd.insertBatch(sqlSession,myMapper,callOutList);
    }

    @Override
    public PageResultSet<CallOut> findByPage(CallOutQuery callOutQuery) {
        if (!StringUtils.isEmpty(callOutQuery.getOrderBy())){
            PageHelper.orderBy(callOutQuery.getOrderBy());
        }
        PageHelper.offsetPage(callOutQuery.getOffset(),callOutQuery.getLimit());
        Weekend<CallOut> example = Weekend.of(CallOut.class);
        WeekendCriteria<CallOut,Object> criteria = example.weekendCriteria();

        //todo：日期大于，注意为字符串比较大小，是否要转换为日期型,小于和等于
        // log.info("{}",bussItemQuery.getSearchDate());
        if (!StringUtils.isEmpty(callOutQuery.getBeginDate())){
            criteria.andGreaterThanOrEqualTo(CallOut::getHandlerdate,callOutQuery.getBeginDate());
        }
        //小于截至日期
        if (!StringUtils.isEmpty(callOutQuery.getEndDate())){
            criteria.andLessThanOrEqualTo(CallOut::getHandlerdate,callOutQuery.getEndDate());
        }
        if (!StringUtils.isEmpty(callOutQuery.getPhone())){
            criteria.andEqualTo(CallOut::getPhone,callOutQuery.getPhone());
        }
        if (!StringUtils.isEmpty(callOutQuery.getArea())){
            criteria.andLike(CallOut::getArea,"%"+callOutQuery.getArea()+"%");
        }
        if (!StringUtils.isEmpty(callOutQuery.getHandlerResult())){
            criteria.andLike(CallOut::getHandlerResult,"%"+callOutQuery.getHandlerResult()+"%");
        }

        PageResultSet<CallOut> resultSet = new PageResultSet<>();
        resultSet.setRows(callOutMapper.selectByExample(example));
        resultSet.setTotal(callOutMapper.selectCountByExample(example));
        return resultSet;
    }

    @Override
    public void importExcel2DataBase(String pathFile) {
        BigExcelUtil bigExcelUtil =  BigExcelUtil.getInstance();
        List<Object> objs = bigExcelUtil.importExcel2Obj(pathFile,CallOut.class);
        List<CallOut> callOutList = objs.stream()
                .filter(o -> {
                    if (!StringUtils.isEmpty(((CallOut)o).getPhone())) return true;
                    else return false;
                })
                .map( o ->{
                    return (CallOut)o;
                }).collect(Collectors.toList());
        creatbatch(callOutList);
    }
}
